package com.iceclean.daoImpl;

import com.iceclean.dao.PostCommentDao;
import com.iceclean.po.PostComment;
import com.iceclean.util.JdbcUtil;

import java.util.List;

/**
 * @author : Ice'Clean
 * @date : 2021-05-09
 */
public class PostCommentDaoImpl implements PostCommentDao {

    @Override
    public PostComment findPostCommentById(int commentId) {
        List<PostComment> postCommentList = JdbcUtil.query("select * from t_post_comment where comment_id = ?", PostComment.class, commentId);
        return postCommentList.size() == 0 ? null : postCommentList.get(0);
    }

    @Override
    public List<PostComment> findPostCommentByRoot(int rootCommentId) {
        List<PostComment> postCommentList = JdbcUtil.query("select * from t_post_comment where root_comment_id = ?", PostComment.class, rootCommentId);
        return postCommentList.size() == 0 ? null : postCommentList;
    }

    @Override
    public List<PostComment> findPostCommentByPage(int postId, int page, int num) {
        // 按时间降序的方法找，新发布的在前面（要求回复的根回复为 null，才算是根回复）
        List<PostComment> postCommentList = JdbcUtil.query("select * from t_post_comment where post_id = ? and root_comment_id is null order by create_date desc limit " + (page-1)*num + "," + num, PostComment.class, postId);
        return postCommentList.size() == 0 ? null : postCommentList;
    }

    @Override
    public List<PostComment> findPostCommentByPage(int postId, int rootCommentId, int page, int num) {
        // 按时间降序的方法找，新发布的在前面
        List<PostComment> postCommentList = JdbcUtil.query("select * from t_post_comment where post_id = ? and root_comment_id = ? order by create_date desc limit " + (page-1)*num + "," + num, PostComment.class, postId, rootCommentId);
        return postCommentList.size() == 0 ? null : postCommentList;
    }

    @Override
    public int createPostComment(PostComment postComment) {
        return JdbcUtil.insert("t_post_comment", postComment);
    }

    @Override
    public boolean updatePostComment(PostComment postComment) {
        return JdbcUtil.update("t_post_comment", "comment_id = " + postComment.getCommentId(), postComment);
    }

    @Override
    public boolean deletePostComment(int postCommentId) {
        return JdbcUtil.delete("t_post_comment", "comment_id = " + postCommentId);
    }
}
